clear
set more off

* ------------------------------------------------------------------------------
* set paths
global root "[insert path here]"
global input "$root/Input" 
global output "$root/Output"
global intermediate "$root/Intermediate"
global figures "$root/Figures"

* ==============================================================================
* 			  Hedonic regressions (direct effect of xeriscape)
* ==============================================================================
* define regression conditions
local na "addition == 0 & addition2 == 0 & addition3 == 0"
local limitp199na "price_199==1 & `na'"
local limitp199pre7na "price_199==1 & yros<2007 & `na'"
local limiters "p199na p199pre7na"
local g1cntrlVar "age landsf housesf bed fbath hbath poolsf garsf_1"
local dnf "filter == 0 & impure == 0"

* define fixed effects
local fe3 "i.yros#i.quart#i.blk10" // quarter-block
local fe3p "i.yros#i.quart#i.blk10 i.parcel2" // quarter-block and parcel

use "$input/hedonic_panel.dta", clear

* quarter-block fixed effects (full sample)
local opt "p199na"
reghdfe lnp participant xeric adjacent axeric `g1cntrlVar' if `limit`opt'', /// 
a(`fe3') vce(cl blk10)
estimates save "$output/`opt'_fe3.ster", replace

* quarter-block fixed effects (sample restricted to 2006)
local opt "p199pre7na"
reghdfe lnp participant xeric adjacent axeric `g1cntrlVar' if `limit`opt'', /// 
a(`fe3') vce(cl blk10)
estimates save "$output/`opt'_fe3.ster", replace
	
* repeat sales (full sample)
local opt "p199na"
reghdfe lnp xeric axeric if `limit`opt'', a(`fe3p') vce(cl blk10)
estimates save "$output/`opt'_fe3p.ster", replace
 
* quarter-block fixed effects (full sample - dnf)
local opt "p199na"
reghdfe lnp participant xeric adjacent axeric `g1cntrlVar' ///
if `limit`opt'' & `dnf', a(`fe3') vce(cl blk10)
estimates save "$output/`opt'_fe3_dnf.ster", replace

* repeat sales (full sample - dnf)
local opt "p199na"
reghdfe lnp xeric axeric if `limit`opt'' & `dnf', a(`fe3p') vce(cl blk10) 
estimates save "$output/`opt'_fe3p_dnf.ster", replace

* ==============================================================================
* 			 Create tables
* ==============================================================================

*_______________________________________________________________________________
* Create table 6
set more off
clear matrix

use "$input/hedonic_panel.dta", clear

* list of variables with variable names for summary statistics tables
local vlist "price age landsf housesf bed fbath hbath pool garsf_1"
local rnames "sale price" "age" "lot sqft" "home sqft" "bedrooms" "full bath" "half bath" "pool" "garage sqft"

local tbloption1b "tex fragment replace coljust(rl) hlines(101{0}11) spacebef(111{0}1)"
local tbloption2 "sdec(0,0,0,2,0,0,2\1,1,1,2,1,1,2\0,0,0,2,0,0,2\0,0,0,2,0,0,2\2,2,2,2,2,2,2\2,2,2,2,2,2,2\2,2,2,2,2,2,2\2,2,2,2,2,2,2\0,0,0,2,0,0,2)"

local histop "density start(0) graphregion(color(white)) ylabel(, nogrid)"
local limiter "p199na"
local feOption "all reps"

* ------------------------------------------------------------------------------
* recover estimation sample and define indicator for all sales
estimates use "$output/`limiter'_fe3.ster"
quietly `e(cmdline)'
gen esam_all = e(sample)
tab esam_all, m

* recover estimation sample and define indicator for repeat sales
estimates use "$output/`limiter'_fe3p.ster"
quietly `e(cmdline)'
gen esam_reps = e(sample)
tab esam_reps, m

* ------------------------------------------------------------------------------
* explore census block tracks with all sales sample
preserve
keep if esam_all == 1
codebook blk10 // check if any parcels are unassigned to blocks
keep blk10 parcel
bys blk10: gen numblks = _n 
by blk10: egen numparcel = max(numblks)
duplicates drop blk10, force
tabstat numparcel if blk10!=., stat(N mean min max sd)
restore

* explore census block tracks with repeat sales sample
preserve
keep if esam_reps == 1
codebook blk10 // check if any parcels are unassigned to blocks
keep blk10 parcel
bys blk10: gen numblks = _n 
by blk10: egen numparcel = max(numblks)
duplicates drop blk10, force
tabstat numparcel if blk10!=., stat(N mean min max sd)
restore

* ------------------------------------------------------------------------------
* explore pools
gen pool = 0
replace pool = 1 if poolcnt > 0

* ------------------------------------------------------------------------------
* explore garages
gen garage = 0
replace garage = 1 if garsf_1 > 0

* ------------------------------------------------------------------------------
* create summary stats table

local j = 0
foreach sales of local feOption {
display " --------------- Fixed-effect option: `sales' sales --------------- "

local j = `j' + 1
if `j' == 1 {
	local panel = "(a) All sales"
	}
else if `j' == 2 {
	local panel = "(b) Repeat sales"
	}

* define number of rows for the table
local i = 0
foreach v of varlist `vlist' {
	local i = `i' + 1
}
mat T = J(`i',7,.)

* populate matrix 
local i = 0
foreach v of varlist `vlist' {
	local i = `i' + 1
	* xeric
	quietly summarize `v' if esam_`sales' == 1
	mat T[`i',1] = r(mean)
	quietly ttest `v' if esam_`sales' == 1, by(xeric)
	display "`v' (converter sales sample): p value of difference `r(p)'"
	mat T[`i',2] = r(mu_1)
	mat T[`i',3] = r(mu_2)
	mat T[`i',4] = r(p)
	* axeric
	quietly ttest `v' if esam_`sales' == 1, by(axeric)
	display "`v' (neighbor sales sample): p value of difference `r(p)'"
	mat T[`i',5] = r(mu_1)
	mat T[`i',6] = r(mu_2)
	mat T[`i',7] = r(p)
}
quietly summarize xeric if esam_`sales' == 1
local nfull : display %12.0fc `r(N)'
quietly summarize xeric if xeric==1 & esam_`sales' == 1
local nxeric1 : display %12.0fc `r(N)'
quietly summarize xeric if xeric==0 & esam_`sales' == 1
local nxeric0 : display %12.0fc `r(N)'

quietly summarize axeric if esam_`sales' == 1
local nfull_adj : display %12.0fc `r(N)'
quietly summarize axeric if axeric==1 & esam_`sales' == 1
local naxeric1_adj : display %12.0fc `r(N)'
quietly summarize axeric if axeric==0 & esam_`sales' == 1
local naxeric0_adj : display %12.0fc `r(N)'

* output matrix to table
mat rownames T = "`rnames'"
frmttable using "$output/table_6`sales'.tex", statmat(T) ///
`tbloption1b' `tbloption2' ///
ctitle("{\bf `panel'}","","\emph{Participant}","","","\emph{Neighbor}","","" \ ///
"","Full","\(P=0\)","\(P=1\)","p-val","\(N=0\)","\(N=1\)","p-val") ///
multicol(1,3,3;1,6,3) vlines(00000100) ///
addrows("Observations","`nfull'","`nxeric0'","`nxeric1'","","`naxeric0_adj'","`naxeric1_adj'","")
}

*_______________________________________________________________________________
* Create table 7

local vlist1 "age landsf housesf bed fbath hbath poolsf garsf_1"
local tblOP "b(a2 a2 a2 a2 a2 %8.7e %8.7e a2 a2 a2 %8.7e %8.7e) se staraux nomti lines nonotes starlevels(* 0.10 ** 0.05 *** 0.01)"
local partlab "\(DP\) (ever converts)"
local adjlab "\(DN\) (neighbors \(DP\))"
local postconvert "Direct effect"
local postadj "Spillover effect"
local sp "\hspace{1mm}"
local qbFE "`sp' quarter-block"

local notestar "\sym{*}\(p<0.10\), \sym{**}\(p<0.05\), \sym{***}\(p<0.01\)"
local notetrim "2014 adjusted sale prices trimmed at the 1$^{st}$ and 99$^{th}$ percentiles."
local noteFE "Fixed-effects: 2010 U.S. Census Blocks (blocks) by quarter of sample (e.g. 1$^{st}$ quarter of 1997 is quarter 5)."
local notena "Sample excludes parcels undertaking additions (see section \ref{ch1sec:wtp-data})."
local noteSEpaper "Census Block culstered standard errors."
local notednf "The DNF sample includes a control sample of parcels that were approved for the rebate, but did not finish."

local allcntrl "All"
local dnfcntrl "DNF"

* ==============================================================================
* Regression output tables: main specification
* ==============================================================================
set more off
clear

estimates use "$output/p199na_fe3.ster"
quietly estadd local cntrl "`allcntrl'"
quietly estadd local FEname "", replace
quietly estadd local samp "1996-2014", replace
quietly estadd local parcel "-", replace
quietly estadd local qb "yes", replace
eststo p199nafe3

estimates use "$output/p199pre7na_fe3.ster"
quietly estadd local cntrl "`allcntrl'"
quietly estadd local FEname "", replace
quietly estadd local samp "1996-2006", replace
quietly estadd local parcel "-", replace
quietly estadd local qb "yes", replace
eststo p199pre7nafe3

estimates use "$output/p199na_fe3p.ster"
quietly estadd local cntrl "`allcntrl'"
quietly estadd local FEname "", replace
quietly estadd local samp "1996-2014", replace
quietly estadd local parcel "yes", replace
quietly estadd local qb "yes", replace
eststo p199nafe3p

// load in the DNF ster files
estimates use "$output/p199na_fe3_dnf.ster"
quietly estadd local cntrl "`dnfcntrl'"
quietly estadd local FEname "", replace
quietly estadd local samp "1996-2014", replace
quietly estadd local parcel "-", replace
quietly estadd local qb "yes", replace
eststo p199nafe3dnf

estimates use "$output/p199na_fe3p_dnf.ster"
quietly estadd local cntrl "`dnfcntrl'"
quietly estadd local FEname "", replace
quietly estadd local samp "1996-2014", replace
quietly estadd local parcel "yes", replace
quietly estadd local qb "yes", replace
eststo p199nafe3pdnf

*__________________________________________________
* main specification: (all)
esttab p199nafe3 p199pre7nafe3 p199nafe3p p199nafe3dnf p199nafe3pdnf using ///
 "$output/table_7_all.tex", substitute(\_ _) ///
 replace keep(participant xeric adjacent axeric `vlist1') ///
 `tblOP' stats(cntrl samp FEname qb parcel r2_a N, ///
 fmt(%15s %15s %15s %15s %15s %12.2f %12.0fc) ///
 label("Control group" "Sale years" "\emph{Fixed-effects}" "`qbFE'" ///
 "`sp' parcel" "adj. \emph{R}$^2$" "Observations")) ///
 coeflabels( ///
 participant "`partlab'" xeric "`postconvert'" adjacent ///
 "`adjlab'" axeric "`postadj'" ///
 age "age (years)" landsf "parcel sqft" housesf "house sqft" bed bedrooms ///
 fbath "full bath" hbath "half bath" poolsf "pool sqft" garsf_1 ///
 "garage sqft") ///
 addnotes("`notestar'. `notetrim'" "`noteFE'" "`notena' `noteSEpaper'" "`notednf'") 
 
* main specification: (all)
esttab p199nafe3 p199pre7nafe3 p199nafe3p p199nafe3dnf p199nafe3pdnf using ///
 "$output/table_7.tex", substitute(\_ _) ///
 replace keep(participant xeric adjacent axeric) ///
 `tblOP' stats(cntrl samp FEname qb parcel r2_a N, ///
 fmt(%15s %15s %15s %15s %15s %12.2f %12.0fc) ///
 label("Control group" "Sale years" "\emph{Fixed-effects}" "`qbFE'" ///
 "`sp' parcel" "adj. \emph{R}$^2$" "Observations")) ///
 coeflabels( ///
 participant "`partlab'" xeric "`postconvert'" adjacent ///
 "`adjlab'" axeric "`postadj'" ///
 ) ///
 addnotes("`notestar'. `notetrim'" "`noteFE'" "`notena' `noteSEpaper'" "`notednf'") 
 
* main specification: (all)
esttab p199nafe3 p199pre7nafe3 p199nafe3p p199nafe3dnf p199nafe3pdnf using ///
 "$output/table_7_controlsOnly.tex", substitute(\_ _) ///
 replace keep(`vlist1') ///
 `tblOP' stats(cntrl samp FEname qb parcel r2_a N, ///
 fmt(%15s %15s %15s %15s %15s %12.2f %12.0fc) ///
 label("Control group" "Sale years" "\emph{Fixed-effects}" "`qbFE'" ///
 "`sp' parcel" "adj. \emph{R}$^2$" "Observations")) ///
 coeflabels( ///
 age "age (years)" landsf "parcel sqft" housesf "house sqft" bed bedrooms ///
 fbath "full bath" hbath "half bath" poolsf "pool sqft" garsf_1 ///
 "garage sqft") ///
 addnotes("`notestar'. `notetrim'" "`noteFE'" "`notena' `noteSEpaper'" "`notednf'")
